package com.example;

/**
 * @desc:
 * @author: Shoko
 * @date: 2025/6/6
 */
public class DeleteCommand implements ResourceCommand {
    private AddressBook addressBook;
    private String name;
    private String deletedPhone;
    private boolean executed = false;

    public DeleteCommand(AddressBook addressBook, String name) {
        this.addressBook = addressBook;
        this.name = name;
    }

    @Override
    public void execute() {
        deletedPhone = addressBook.viewContact(name);
        if (deletedPhone != null) {
            addressBook.deleteContact(name);
            executed = true;
        }
    }

    @Override
    public void undo() {
        if (executed && deletedPhone != null) {
            addressBook.addContact(name, deletedPhone);
            executed = false;
        }
    }
}